package com.zjmzxfzhl.modules.app.service.impl;

import com.zjmzxfzhl.modules.app.entity.AppLog;
import com.zjmzxfzhl.modules.app.entity.AppTask;
import com.zjmzxfzhl.modules.app.form.AppMptVo;
import com.zjmzxfzhl.modules.app.service.AppTaskService;
import com.zjmzxfzhl.modules.app.service.AppTaskUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zjmzxfzhl.common.core.base.BaseServiceImpl;
import com.zjmzxfzhl.modules.app.entity.AppMpt;
import com.zjmzxfzhl.modules.app.mapper.AppMptMapper;
import com.zjmzxfzhl.modules.app.service.AppMptService;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * Service
 * 
 * @author 张森荣
 */
@Service
public class AppMptServiceImpl extends BaseServiceImpl<AppMptMapper, AppMpt>
        implements AppMptService {
    @Autowired
    private AppTaskService appTaskService;

    @Override
    public IPage<AppMptVo> list(IPage<AppMptVo> page, AppMptVo appMptVo) {
        return page.setRecords(baseMapper.list(page, appMptVo));
    }

    /**
     * 已拒绝
     *
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean close(String mptId) {
        AppMpt appMpt=this.getById(mptId);
        if(appMpt==null){
            return false;
        }
        appMpt.setStatus("02");
        appMpt.setUpdateTime(new Date());
        Boolean result = this.updateById(appMpt);
        return result;
    }

    /**
     * 更改状态为已完成
     *
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void finish(String mptId) {
        AppMpt appMpt=this.getById(mptId);
        if(appMpt==null){
            return ;
        }
        //已关闭里程碑不做处理
        if("02".equals(appMpt.getStatus())){
            return ;
        }

        int notFinishNum= appTaskService.notFinishCount(mptId);
        if(notFinishNum<=0){
            appMpt.setStatus("04");
        }
        appMpt.setUpdateTime(new Date());
        this.updateById(appMpt);
    }

    /**
     * 更改状态为已逾期
     *
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void timeout(String mptId) {
        AppMpt appMpt=this.getById(mptId);
        if(appMpt==null){
            return ;
        }
        //已关闭里程碑不做处理
        if("02".equals(appMpt.getStatus())){
            return ;
        }

        int notFinishNum= appTaskService.timeoutCount(mptId);
        if (notFinishNum > 0) {
            appMpt.setStatus("03");
        }
        appMpt.setUpdateTime(new Date());
        this.updateById(appMpt);
    }

    /**
     * 更改状态为进行中
     *
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void inProgress(String mptId) {
        AppMpt appMpt=this.getById(mptId);
        if(appMpt==null){
            return ;
        }
        //已关闭里程碑不做处理
        if("02".equals(appMpt.getStatus())){
            return ;
        }
        if (appMpt.getStartDate() != null) {
            //时间到时自动进行里程碑
            if (new Date().getTime() >= appMpt.getStartDate().getTime()) {
                appMpt.setStatus("01");
            }
        }
        appMpt.setUpdateTime(new Date());
        this.updateById(appMpt);
    }

    /**
     * 开启
     *
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean open(String mptId) {
        AppMpt appMpt=this.getById(mptId);
        if(appMpt==null){
            return false;
        }
        //修改为进行中或待确认，判断是否逾期
        if (appMpt.getStartDate() != null) {
            //时间到时自动进行里程碑
            if (new Date().getTime() >= appMpt.getStartDate().getTime()) {
                appMpt.setStatus("01");
            }
        }
        if("02".equals(appMpt.getStatus())){
            appMpt.setStatus("00");
        }
        appMpt.setUpdateTime(new Date());
        Boolean result = this.updateById(appMpt);
        timeout(mptId);
        return result;
    }
}
